package com.bangf.helper;

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 雷楚桥
 * @deprecation 时间戳助手函数
 * @email leichuqiao@126.com
 * @date 2018-06-14 19:53
 */
@Component
public class TimeStampHelper {

    @PostConstruct
    public void init(){}


    public String dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    public String stampToDate(String s) {
        return stampToDate(s, 0);
    }

    public String stampToDate(String s, Integer type) {

        String pattern = "";
        if (type == 0)
            pattern = "yyyy-MM-dd HH:mm:ss";
        else if (type == 1) {
            pattern = "yyyy-MM-dd";
        }else if(type == 2){
            pattern = "yyyy年MM月dd日 HH:mm:ss";
        }
        String res;
        if ("".equals(s) || s == null)
            return "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        long lt = s.length() > 11 ? new Long(s) : new Long(s + "000");
        Date date = new Date(lt);

        res = simpleDateFormat.format(date);
        return res;
    }

    public String getTimeStamp() {
        long timeMillis = System.currentTimeMillis();
        return timeMillis / 1000 + "";
    }
}
